//
//  GuestOrderingView.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-14.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GuestOrderingView.h"
#import "Imports1.h"
#import "PopoverTableViewController.h"
#import "PopoverLanguageSelect.h"
#import "PopoverOtherFunctions.h"

@interface GuestOrderingView(Private)
-(void) removeNineModeIntoCommend;
-(void) removeCommendIntoNineMode;
@end

@implementation GuestOrderingView
@synthesize guestCommendView;
@synthesize guestNineModeView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未结束...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    UIButton *btn = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:bid];
    if (bid == TagOfUIButton) {//home
        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"GuestOrderingView", @"FromNibName", @"MenuBookChoose",@"ToNibName",nil];
        postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);       
    }else if(bid ==  TagOfUIButton + 1){//search
        PopoverTableViewController *ptvc = [[PopoverTableViewController alloc] initWithNibName:@"PopoverTableViewController" bundle:nil popoverType:3];
        [ptvc PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView] directions:UIPopoverArrowDirectionUp];
        [ptvc release];  
    }else if(bid ==  TagOfUIButton + 2){//推荐菜 
        if ([AppSession.commendPluPlus count]>0) {
            [AppSession initCommendPluPlus];
            postN(@"SysMsg_GuestCommendView_refreshCommendView");
            [self removeNineModeIntoCommend];
        }else {
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                            message:TranslateStr(@"没有推荐菜")
                                                           delegate:nil 
                                                  cancelButtonTitle:TranslateStr(@"确定") 
                                                  otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }    
    else if(bid == TagOfUIButton + 7){//myorder
        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"GuestOrderingView", @"FromNibName", @"MyOrderView",@"ToNibName",nil];
        postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);
    }else if(bid == TagOfUIButton + 8){//languge
        PopoverLanguageSelect *pls = [[PopoverLanguageSelect alloc] initWithNibName:@"PopoverLanguageSelect" bundle:nil];
        [pls PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView] directions:UIPopoverArrowDirectionUp];
        [pls release];        
    }else if(bid == TagOfUIButton + 9){//more        
        //版本二
        PopoverOtherFunctions *moof = [[PopoverOtherFunctions alloc] initWithNibName:@"PopoverOtherFunctions" bundle:nil];
        [moof PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView] directions:UIPopoverArrowDirectionUp];
        [moof release];  
    }
}
-(void) removeCommendIntoNineMode{
    [self.view addSubview:guestNineModeView];
    UIButton *btn = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 2];
    btn.hidden = NO;
    btn.enabled = YES;
    AppSession.isCommendView = NO;
}
-(void) removeNineModeIntoCommend{
    [self.view addSubview:guestCommendView];
    UIButton *btn = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 2];
    btn.hidden = YES;
    btn.enabled = NO;
    AppSession.isCommendView = YES;
}
-(void) refreshToolBar{
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+0] setText:TranslateStr(AppSession.currentOrderMenu.ptTableNo)];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+1] setText:AppSession.currentOrderMenu.persons];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+2] setText:TranslateStr(AppSession.currentOrderMenu.peEmpno)];
    
    float myorderCount = [DataBaseAccess getPluBookCount];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+10] setText:[NSString stringWithFormat:@"%d",(int)myorderCount]];
    
    //翻译
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+3] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"桌号")]];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+4] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"人数")]];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+5] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"工号")]];
    
    [(UIButton *)[self.view viewWithTag:TagOfUIButton+2] setTitle:TranslateStr(@"推荐菜") forState:UIControlStateNormal];
    [(UIButton *)[self.view viewWithTag:TagOfUIButton+7] setTitle:TranslateStr(@"我的点单") forState:UIControlStateNormal];
    [(UIButton *)[self.view viewWithTag:TagOfUIButton+8] setTitle:TranslateStr(@"语言") forState:UIControlStateNormal];
    [(UIButton *)[self.view viewWithTag:TagOfUIButton+9] setTitle:TranslateStr(@"更多功能") forState:UIControlStateNormal];
}

#pragma mark - View lifecycle
- (void)viewDidLoad{   
    [AppSession initCommendPluPlus];//初始化推荐菜
    
    addNObserver(@selector(removeCommendIntoNineMode),SysMsg_GuestOrdering_removeCommendIntoNineMode);
    addNObserver(@selector(removeNineModeIntoCommend),SysMsg_GuestOrdering_removeNineModeIntoCommend);
    addNObserver(@selector(refreshToolBar), @"SysMsg_GuestOrdering_refreshToolBar");
    
    if ([AppSession.commendPluPlus count]>0 && AppSession.isCommendView) {
        postN(SysMsg_GuestOrdering_removeNineModeIntoCommend);
    }else {
        postN(SysMsg_GuestOrdering_removeCommendIntoNineMode);
    }
    
    [super viewDidLoad];
}
- (void)viewDidUnload{
    [self setGuestCommendView:nil];
    [self setGuestNineModeView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (void)dealloc {
    MyNSLog(@"GuestOrderingView is deallocing...");
    removeNObserver(SysMsg_GuestOrdering_removeNineModeIntoCommend);
    removeNObserver(SysMsg_GuestOrdering_removeCommendIntoNineMode);
    removeNObserver(@"SysMsg_GuestOrdering_refreshToolBar");
    [guestCommendView release];
    [guestNineModeView release];
    [super dealloc];
}

@end
